function [X, Y, Z] = PlotFunc(P_num, policy, net, states, state_features,out, folder, name)

global home_path;

folder = strcat(home_path, 'plots/SOM/',folder,'/');
% z values on the function plot
z = [];
ranges = [];
minimums = [];
maximums = [];

% matrix of features for each state in the input
if(isempty(state_features))
    disp('Converting states into features...');
    state_features = StatesToFeatures(states);
end

if(isempty(out))
    out = sim(net, state_features');
end

output_size = size(out);

num_clusters = output_size(1);

disp('Getting state values...');
for cluster = 1:num_clusters, 
    [neighbours, values] = FindNeighbours(P_num, out(cluster,:), states, policy);
    if(isempty(values)), 
        z = [z, 0]; 
        minimums = [minimums, 0];
        maximums = [maximums, 0];
        ranges = [ranges,0];
    else
        z = [z, mean(values)];
        minimum = min(values);
        maximum = max(values);
        minimums = [minimums, minimum];
        maximums = [maximums, maximum];
        ranges = [ranges, abs(maximum-minimum)];
    end
end

% create the mesh
[X,Y,Z, ranges] = ProducePlotMesh(net,z, ranges);

% disp('Plotting...');
% % plot the 3D function
figure(1)
surf(X,Y,Z);
colorbar;
figure(2)
contourf(X,Y,Z);
colorbar;
figure(3)
pcolor(X,Y,Z)
colorbar;
% 
% disp('Saving plots...');
% SaveAllFigures(folder, name, 'png');
% SaveAllFigures(folder, name, 'fig');

%plotsomhits(net, state_features');
%plotsomnd(net)

end


